package com.wqiuqiu.gulimall.order.listener;


import com.wqiuqiu.common.to.mq.SecdKillOrderTo;
import com.wqiuqiu.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;

import java.io.IOException;


@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSecdKillListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(SecdKillOrderTo to, Channel channel, Message message) throws IOException {
        log.info("创建秒杀订单...");
        try {
            //非批量接收 false，消息全部都在channel里面
            orderService.createSeckillOrder(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //是否重新入队，如果为true，他会立即被再次消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
